package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.BaseResult;
import com.qf.pojo.Category;
import com.qf.service.CategoryService;
import com.qf.service.impl.CategoryServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 分类的后端访问地址
 */
@WebServlet("/category")
public class CategoryServlet extends BaseServlet {

    /**
     * 获取所有的分类信息
     * @param request 请求对象
     * @param response 响应对象
     */
    public void getAllCategories(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //调用分类的业务接口
        CategoryService categoryService =  new CategoryServiceImpl() ;
//        List<Category> allCategories = categoryService.getAllCategories();
        BaseResult result = categoryService.getAllCategories();
        String str = null ;
        if(result!=null){
            //将集合对象解析json
            JSONObject jsonObject = new JSONObject() ;
             str = jsonObject.toJSONString(result);
        }
        /*System.out.println(allCategories);
        String str = null ;
        if(allCategories!=null){
            //存在
            //将集合对象解析json
            JSONObject jsonObject = new JSONObject() ;
            str = jsonObject.toJSONString(allCategories);
        }*/

        //响应给前端
        response.getWriter().write(str);
    }

}
